package com.zhouxianglh.p199;

import java.util.ArrayList;
import java.util.List;


/**
 * 199. 二叉树的右视图
 * <p>
 * 递归遍历所有节点,取每一级最右的结果
 *
 * @author zhouxianglh
 */
public class Solution {
    public static List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (null != root) {
            getTreeValue(root, 1, list);
        }
        return list;
    }

    public static void getTreeValue(TreeNode node, int level, List<Integer> list) {
        if (list.size() < level) {
            list.add(node.val);
        } else if (null == list.get(level - 1)) {
            list.add(node.val);
        }
        if (null != node.right) {
            getTreeValue(node.right, level + 1, list);
        }
        if (null != node.left) {
            getTreeValue(node.left, level + 1, list);
        }
    }

    public static class TreeNode {
        int val;

        TreeNode left;

        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
